package com.itheima.countroller;

import com.itheima.cammon.CommonResult;
import com.itheima.cammon.PageInof;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王同学
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    /*套餐管理控制层*/
    @Autowired
    SetmealService setmealService; //如果这边爆红的话，那就是接口没有去写实现方法

    //添加新套餐
    @PostMapping
    public CommonResult add(@RequestBody Setmeal stemeal) {
        //先实现，判断后面再去补齐
        boolean add = setmealService.add(stemeal);
        if (!add) {
            throw new RuntimeException("新增套餐失败~！");
        }
        return CommonResult.success("新增套餐成功~！");
    }

    //分页查询 // TODO: 2022/12/4 待修复
    @GetMapping("page")
    public CommonResult findpage(String name, @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInof<Setmeal> res = setmealService.page(name, page, pageSize);
        return CommonResult.success(res);
    }

    //删除套餐
    @DeleteMapping
    public CommonResult delete(@RequestParam String ids) {
        //ids复数形式去写，表示不止一个元素 根据套餐id去删除套餐和菜品表
        boolean res = setmealService.deleteById(ids);
        if (!res) {
            return CommonResult.error("删除失败~！");
        }
        return CommonResult.success("删除成功~！");
    }

    //根据套餐id查询 该id下的菜品
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable String id) {
        if (id == null || id == "") {
            throw new RuntimeException("要查询的id数据丢失");
        }
        Setmeal res = setmealService.findById(id);
        return CommonResult.success(res);
    }

    //修改套餐
    @PutMapping
    public CommonResult update(@RequestBody Setmeal setmeal) {
        if (setmeal == null) {
            throw new RuntimeException("修改的数据丢失！");
        }
        boolean res = setmealService.update(setmeal);
        return CommonResult.success(res);
    }

    //设置套餐上架下架
    @PostMapping("status/{status}")
    public CommonResult status(@PathVariable int status, String ids) {
        boolean number = setmealService.updatestatus(ids, status);
        if (!number) {
            CommonResult.error("售卖状态修改失败~！！");
        }

        return CommonResult.success("售卖状态修改成功~！！");
    }


}
